package com.xpec.c4.game.datamanager.agent.money;

import java.math.BigDecimal;

import com.ms.player.Player;
import com.ms.service.module.zone.command.BasicDebugCommand;
import com.ms.service.module.zone.command.CommandHelper;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.game.common.Helper;
import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.utility.GUIDFactory;
import com.xpec.c4.game.utility.GUIDType;
import com.xpec.c4.game.utility.GameUtility;

/**
 * 測試拉彩公告
 * 
 * @author kvn_yang
 */
public class AddMoneyCommand extends BasicDebugCommand {

	/** 試玩幣 */
	static final private int PARAM_TRIAL_COIN = 1;

	/** 金幣 */
	static final private int PARAM_GOLD_COIN = 2;

	/** 幸運金 */
	static final private int PARAM_LUCKY_COIN = 3;

	/** 所有的金 */
	static final private int PARAM_ALL_COIN = 4;

	/** 金錢異動值在命令字串中的位置 */
	static final private int INDEX_CHANGE_VALUE = 2;

	private CommandHelper helper = new CommandHelper(getName());

	public AddMoneyCommand() {
		helper.addParam(PARAM_TRIAL_COIN, "trial", "[value]");
		helper.addParam(PARAM_GOLD_COIN, "gold", "[value]");
		helper.addParam(PARAM_LUCKY_COIN, "lucky", "[value]");
		helper.addParam(PARAM_ALL_COIN, "all", "[value]");
	}

	@Override
	public String execute() {
		int index = helper.getCommandIndex(commandValue);

		BasicZoneModule module = getModule();
		if (module == null) {
			return "can't get current module.";
		}

		Player player = getPlayer();
		if (player == null) {
			return "can't get current player.";
		}

		if (!GameUtility.isHaveString(getParam(INDEX_CHANGE_VALUE))) {
			return getDescription();
		}

		// 變更金額
		BigDecimal changeValue = new BigDecimal(getParam(INDEX_CHANGE_VALUE));

		// 金錢異動行為
		MoneyChangeWay changeWay;
		if (Helper.isGreaterThanZero(changeValue)) {
			changeWay = MoneyChangeWay.ADD_TEST_COMMAND;
		} else if (Helper.isMinus(changeValue)) {
			changeWay = MoneyChangeWay.SUB_TEST_COMMAND;
			changeValue = Helper.mul(changeValue, new BigDecimal(-1));
		} else {
			return "value can't be 0";
		}

		
		String gameId = "";
		String gameName = "";
		String gameHallId = "";
		String gameHallName = "";

		ChangeCoinLister coinLister = new ChangeCoinLister();

		// 幣別
		switch (index) {
		case PARAM_TRIAL_COIN:
			coinLister.addCoin(CoinType.TRIAL, changeValue);
			break;
		case PARAM_GOLD_COIN:
			coinLister.addCoin(CoinType.GOLD, changeValue);
			break;
		case PARAM_LUCKY_COIN:
			coinLister.addCoin(CoinType.LUCKY, changeValue);
			break;
		case PARAM_ALL_COIN:
			coinLister.addCoin(CoinType.TRIAL, changeValue);
			coinLister.addCoin(CoinType.GOLD, changeValue);
			coinLister.addCoin(CoinType.LUCKY, changeValue);
			break;
		default:
			return getDescription();
		}

		// 向 dc 要求變更
		// callback BasicZone.onAddMoneyCommandToDcCallback()
		module.getPlayerDataAgent().updatePlayerMoneyToDC(GUIDFactory.createUUID(GUIDType.TYPE_COMMAND_ORDER_ID),
				player, coinLister, changeWay, MoneyChangeParam.NO_JACKPOT, false, null, null, module, gameId,
				gameName, gameHallId, gameHallName, null, this, "onAddMoneyCommandToDcCallback");

		return "ok";
	}

	@Override
	public String getDescription() {
		return getName() + " [trial / gold / lucky / all] [value / -value]";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ms.service.module.zone.command.BasicDebugCommand#getShortDescription
	 * ()
	 */
	@Override
	public String getShortDescription() {
		return "add money to player.";
	}

	@Override
	public String getName() {
		return "addmoney";
	}

	/**
	 * 金錢異動測試命令的 callback
	 * 
	 * @param obj
	 * @param userObject
	 * @see AddMoneyCommand
	 */
	public void onAddMoneyCommandToDcCallback(BaseDataInfo obj, Object... userObject) {
		MoneyChangeParam param = (MoneyChangeParam) obj.getData();
		Player player = getPlayer();
		if (player == null) {
			return;
		}

		sendDebugCommandResponse(player.getSessionID(), param.getResultString());
	}
}
